﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace iNotesNotification
{
    public partial class iNotesSettingsControl : UserControl
    {
        private bool _isLoaded = false;

        public event EventHandler SettingsSaved;
        
        protected void OnSettingsSaved() 
        {
            if (SettingsSaved != null) SettingsSaved(this, EventArgs.Empty);
        }

        public iNotesSettingsControl()
        {
            InitializeComponent();
        }

        private void iNotesSettingsControl_Load(object sender, EventArgs e)
        {
            PopulateForm();
        }

        public void Save()
        {
            if (!_isLoaded) PopulateForm();
            PopulateConfig();
        }

        private void PopulateForm()
        {
            string browser, notesui;
            int i;

            iNotesConfig conf = iNotesConfig.Instance;

            this.textBoxLogin.Text = conf.Login;
            this.textBoxPassword.Text = string.Empty;
            this.textBoxXMLFeed.Text = conf.XmlUrlSuffix;
            this.textBoxBaseUrl.Text = conf.NotesFileUrl;
            this.textBoxMailList.Text = conf.HtmlUrlSuffix;
            this.textBoxBrowserOverride.Text = conf.BrowserOverrideExePath;
            this.textBoxMailAlertSound.Text = conf.SoundPath;

            if (conf.MailCheckInterval > 0) this.numericUpDownMailCheckSeconds.Value = conf.MailCheckInterval;
            if (conf.DaysToRetrieve > 0) this.numericUpDownDaysToRetrieve.Value = conf.DaysToRetrieve;
            if (conf.CountToRetrieve > 0) this.numericUpDownCountToRetrieve.Value = conf.CountToRetrieve;

            this.checkBoxServiceStart.Checked = conf.StartOnApplicationStartup;
            this.radioButtonXml.Checked = conf.Interface.Equals("xml", StringComparison.CurrentCultureIgnoreCase);
            this.radioButtonHtml.Checked = !this.radioButtonXml.Checked;

            radioButton_CheckedChanged(radioButtonHtml, EventArgs.Empty);

            switch (conf.Browser)
            {
                case "firefox":
                    browser = "Firefox";
                    break;
                case "chrome":
                    browser = "Chrome";
                    break;
                case "iexplore":
                default:
                    browser = "Internet Explorer";
                    break;
            }

            i = this.comboBoxBrowser.FindString(browser);
            if (i >= 0) this.comboBoxBrowser.SelectedIndex = i;
            else this.comboBoxBrowser.SelectedIndex = 0;

            switch (conf.NotesUI)
            {
                case "inotes":
                    notesui = "iNotes Standard";
                    break;
                case "webmail":
                    notesui = "WebMail";
                    break;
                case "ultralite":
                default:
                    notesui = "UltraLite";
                    break;
            }

            i = this.comboBoxNotesUI.FindString(notesui);
            if (i >= 0) this.comboBoxNotesUI.SelectedIndex = i;
            else this.comboBoxNotesUI.SelectedIndex = 0;

            _isLoaded = true;
        }

        private void PopulateConfig()
        {
            iNotesConfig conf = iNotesConfig.Instance;

            conf.Login = this.textBoxLogin.Text;
            if (this.textBoxPassword.Text != string.Empty) conf.Password = this.textBoxPassword.Text;
            conf.XmlUrlSuffix = this.textBoxXMLFeed.Text;
            conf.NotesFileUrl = this.textBoxBaseUrl.Text;
            conf.HtmlUrlSuffix = this.textBoxMailList.Text;
            conf.MailCheckInterval = Convert.ToInt32(this.numericUpDownMailCheckSeconds.Value);
            conf.DaysToRetrieve = Convert.ToInt32(this.numericUpDownDaysToRetrieve.Value);
            conf.CountToRetrieve = Convert.ToInt32(this.numericUpDownCountToRetrieve.Value);
            conf.StartOnApplicationStartup = this.checkBoxServiceStart.Checked;

            if (string.IsNullOrEmpty(this.textBoxBrowserOverride.Text))
            {
                conf.BrowserOverrideExePath = string.Empty;
            }
            else if (File.Exists(this.textBoxBrowserOverride.Text))
            {
                conf.BrowserOverrideExePath = this.textBoxBrowserOverride.Text;
            }

            if (string.IsNullOrEmpty(this.textBoxMailAlertSound.Text))
            {
                conf.SoundPath = string.Empty;
            }
            else if (File.Exists(this.textBoxMailAlertSound.Text))
            {
                conf.SoundPath = this.textBoxMailAlertSound.Text;
            }

            if (this.radioButtonXml.Checked)
            {
                conf.Interface = "xml";
            }
            else
            {
                conf.Interface = "html";
            }

            switch (this.comboBoxBrowser.SelectedItem.ToString())
            {
                case "Firefox":
                    conf.Browser = "firefox";
                    break;
                case "Chrome":
                    conf.Browser = "chrome";
                    break;
                case "Internet Explorer":
                default:
                    conf.Browser = "iexplore";
                    break;
            }

            switch (this.comboBoxNotesUI.SelectedItem.ToString())
            {
                case "iNotes Standard":
                    conf.NotesUI = "inotes";
                    break;
                case "WebMail":
                    conf.NotesUI = "webmail";
                    break;
                case "UltraLite":
                default:
                    conf.NotesUI = "ultralite";
                    break;
            }

            conf.Save();
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonXml.Checked)
            {
                this.textBoxMailList.Enabled = false;
                this.numericUpDownCountToRetrieve.Enabled = false;

                this.textBoxXMLFeed.Enabled = true;
                this.numericUpDownDaysToRetrieve.Enabled = true;
            }
            else
            {
                this.textBoxMailList.Enabled = true;
                this.numericUpDownCountToRetrieve.Enabled = true;

                this.textBoxXMLFeed.Enabled = false;
                this.numericUpDownDaysToRetrieve.Enabled = false;
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            this.Save();
            this.OnSettingsSaved();
        }

        private void buttonPickBrowserOverride_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxBrowserOverride.Text) &&
                File.Exists(textBoxBrowserOverride.Text))
            {
                openFileDialog.FileName = textBoxBrowserOverride.Text;
            }
            openFileDialog.DefaultExt = ".exe";
            openFileDialog.Title = "Pick Browser Executable";
            openFileDialog.Filter = "Executable Files|*.exe";
            openFileDialog.FilterIndex = 0;
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            if (openFileDialog.ShowDialog(this.ParentForm) == DialogResult.OK)
            {
                this.textBoxBrowserOverride.Text = openFileDialog.FileName;
            }
        }

        private void buttonPickSound_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxMailAlertSound.Text) &&
                File.Exists(textBoxMailAlertSound.Text))
            {
                openFileDialog.FileName = textBoxMailAlertSound.Text;
            }
            openFileDialog.DefaultExt = ".wav";
            openFileDialog.Title = "Pick Audio File";
            openFileDialog.Filter = "Wav Files|*.wav";
            openFileDialog.FilterIndex = 0;
            openFileDialog.InitialDirectory = iNotesController.AssemblyLocation.Directory.FullName;

            if (openFileDialog.ShowDialog(this.ParentForm) == DialogResult.OK)
            {
                this.textBoxMailAlertSound.Text = openFileDialog.FileName;
            }
        }
    }
}
